/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.univaq.iw.jweb.utils;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Stefano
 */
public class ControlsHelpers {

    /* metodo utile per validare la data inserita */
    public static Date isDateValid (String data) {

        SimpleDateFormat sdf1 = new SimpleDateFormat("dd/MM/yyyy");

        Pattern p = Pattern.compile("\\d\\d/\\d\\d/\\d\\d\\d\\d");
        Matcher m = p.matcher(data);

        if (m.matches()){

            /* controllo se la stringa inserita gg/mm/aaaa ha 10 caratteri */
            if (data.length() == 10) {
                /* controllo se la data a dieci caratteri è una data corretta - valida */

                int gg = Integer.parseInt(data.substring(0,2));
                int mm = Integer.parseInt(data.substring(3,5));
                int yyyy = Integer.parseInt(data.substring(6,10));

                GregorianCalendar cal = new GregorianCalendar (yyyy, mm-1, gg);
                cal.setLenient (false);

                try {
                    cal.get(Calendar.DATE);
                    return new Date(sdf1.parse(data).getTime());

                } catch (ParseException e) {
                    return null;
                } catch (IllegalArgumentException e) {
                    return null;
                }
            }
        } 
        return null;
    }

    /* metodo utile per validare il codice CAP inserito */
    public static boolean controllaCAP(String cf) {
        Pattern p = Pattern.compile("[0-9]{5}");
        Matcher m = p.matcher(cf);
        return m.matches();
    }

    /* metodo utile per validare il numero telefonico */
    public static boolean controllaNumeroTelefono(String nt) {
        Pattern p = Pattern.compile("[0-9]*");
        Matcher m = p.matcher(nt);
        return m.matches();
    }

    /* metodo utile per validare il voto */
    public static boolean controllaVoto(String voto) {
        Pattern p = Pattern.compile("[0-9]{2,3}/[0-9]{2,3}");
        Matcher m = p.matcher(voto);
        return m.matches();
    }

    /* metodo utile per validare il voto */
    public static boolean controllaVotazione(String voto) {
        Pattern p = Pattern.compile("[0-9]{2,3}"); /* 110 da 100 a 109 e inferiori */
        Matcher m = p.matcher(voto);
        return m.matches();
    }

    /* metodo utile per validare la mail inserita */
    public static boolean controllaMail(String email) {
        Pattern p = Pattern.compile("[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}"); /* .+@.+\\.[a-z]{2,4} opp .+@.+\\.[a-z]+ opp [a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+/.[a-zA-Z]{2,4} */
        Matcher m = p.matcher(email);
        return m.matches();
    }

    /* metodo utile per validare il codice fiscale */
    public static boolean controllaCF(String cf) {
        Pattern p = Pattern.compile("[a-zA-Z]{6}\\d\\d[a-zA-Z]\\d\\d[a-zA-Z]\\d\\d\\d[a-zA-Z]");
        Matcher m = p.matcher(cf);
        return m.matches();
    }

    /* metodo utile per validare la partita iva */
    public static boolean controllaPI(String pi) {
        Pattern p = Pattern.compile("[0-9]{11}");
        Matcher m = p.matcher(pi);
        return m.matches();
    }

    public static boolean controllaEta(String eta) {
        Pattern p = Pattern.compile("[1]?[0]{2}|[1-9][0-9]?"); /* 100 o inferiori */
        Matcher m = p.matcher(eta);
        return m.matches();
    }

    public static boolean controllaPW(String password) {
        Pattern p = Pattern.compile("[0-9a-zA-Z]{6,}"); /* almeno 6 tra numeri e lettere */
        Matcher m = p.matcher(password);
        return m.matches();
    }

}
